package org.jeecg.modules.commodity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.commodity.entity.Commodity;
import org.jeecg.modules.commodity.mapper.CommodityMapper;
import org.jeecg.modules.commodity.model.CommodityModel;
import org.jeecg.modules.commodity.service.CommodityService;
import org.jeecg.modules.equipment.entity.Equipment;
import org.jeecg.modules.equipment.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: zhuzhuhong
 * @Date: 2019/11/15 14:09
 * @Description:
 */
@Service
public class CommodityServiceImpl
        extends ServiceImpl<CommodityMapper, Commodity>
        implements CommodityService {

    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private EquipmentService equipmentService;
//    @Autowired

    @Override
    public List<CommodityModel> findCommodityModelByCommodityName(String commodityName) {
        try {
            return commodityMapper.selectCommodityModelByCommodityName(commodityName);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public boolean deleteCommodityById(String id) {
        try {
            //先判断当前商品下面是否存在设备，根据商品Id查询设备数量
            QueryWrapper<Equipment> queryWrapper = new QueryWrapper<Equipment>()
                    .eq("commodity_id", id);
            int count = equipmentService.count(queryWrapper);
            if (count <= 0) {
                return this.removeById(id);
            } else {
                return false;
            }
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public String getCommodityIdByCommodityName(String commodityName) {
        return commodityMapper.selectCommodityIdByCommodityName(commodityName);
    }

}
